package com.eascs.workflow.massSms.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.service.ILoanInfoService;
import com.eascs.admin.common.vo.LoanApplyEntity;
import com.eascs.admin.common.vo.LoanUserInfo;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UuidUtil;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.entity.sequence.SequenceEntity;
import com.eascs.workflow.common.entity.sys.WfMessage;
import com.eascs.workflow.common.service.sequence.SequenceService;
import com.eascs.workflow.massSms.dao.IMassSmsDao;
import com.eascs.workflow.massSms.service.IMassSmsService;
import com.eascs.workflow.massSms.vo.MassSmsVo;

@Service
public class MassSmsServiceImpl implements IMassSmsService {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	IMassSmsDao iMassSmsDao;

	@Autowired
	SequenceService  sequenceService;
	
	@Reference
	private ILoanInfoService  loanInfoService;
	
	@Override
	public PageResult<Object[]> getSmsList(MassSmsVo vo) {
		return iMassSmsDao.getSmsList(vo);
	}

	@Override
	public WfMessage findByrequestId(String requestId) {
		return iMassSmsDao.findByrequestId(requestId);
	}

	@Override
	@Transactional(transactionManager ="wfTransactionManager")
	public Map<String, String> saveSms(MassSmsVo vo) {
		Map<String, String> map = new HashMap<String, String>();
		String contactors = vo.getContactors();
		String[] contactor = contactors.split(";");
		StringBuffer s = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String dateNow = sdf.format(new Date());
		String seq = sequenceService.getSeq(SequenceEntity.MASS_SMS_NO, 2,dateNow);
		for (int i = 0; i < contactor.length; i++) {
			if (StringUtils.isNotBlank(contactor[i])) {
				WfMessage wf = null;
				try {
					wf = setEntity(vo);
					wf.setBizCode(seq);
					wf.setReceiveName(contactor[i]);
					wf.setReceiveTelphone(contactor[i]);
					iMassSmsDao.saveSms(wf);
				} catch (Exception e) {
					e.printStackTrace();
					s.append(contactor[i]).append(";");
					map.put("error", s.toString());
					continue;
				}
			}
			map.put("success", contactor[i]);
		}

		return map;
	}

	private WfMessage setEntity(MassSmsVo vo) throws Exception{
		
		//获取已有的单号
		WfMessage wf = new WfMessage();
		wf.setRequestId(UuidUtil.generateShortUuid());
		wf.setSystemCode("群发短信");
		wf.setContent(vo.getContent());
		wf.setRemark(vo.getRemark());
		wf.setSendType(vo.getSendType());
		wf.setRequestDateTime(new Date());
		if(vo!=null&&!StringUtil.isBlank(vo.getScheduleSendDateTime())){
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			wf.setScheduleSendDateTime(sdf1.parse(vo.getScheduleSendDateTime()));
		}
		wf.setCreateTime(new Date());
		if(UserHolder.getUser()!=null){
			wf.setCreator(UserHolder.getUser().getCname());
			wf.setSendName(UserHolder.getUser().getCname());
		}
		return wf;
	}

	
}
